package sharedServlets;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.techventus.server.voice.Voice;

public class SMS extends HttpServlet{
    final static String USER_AGENT = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13";

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Voice voice = new Voice("syncateam@gmail.com","syncahorateam");
		String destinationNumber = req.getParameter("number");
		String txt = req.getParameter("txt");
		Logger.getLogger("SMS").warning("number: "+destinationNumber+" txt: "+txt);
		Logger.getLogger("SMS").warning("error: "+voice.sendSMS(destinationNumber, txt));
		Logger.getLogger("SMS").warning("After send SMS");
		//Logger.getLogger("SMS").warning("get: "+get("https://www.google.com/voice/"));
	}
	static String get(String urlString) throws IOException {
        URL url = new URL(urlString);
        //+ "?auth=" + URLEncoder.encode(authToken, enc));

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty( "Authorization",
        "GoogleLogin auth="+C2DM.getInst().getToken());
        conn.setRequestProperty(
                                        "User-agent",
                                        USER_AGENT);
        conn.setInstanceFollowRedirects(false); // will follow redirects of same protocol http to http, but does not follow from http to https for example if set to true

        // Get the response
        conn.connect();
        int responseCode = conn.getResponseCode();
        InputStream is;
        if(responseCode==200) {
                is = conn.getInputStream();
        } else if(responseCode==HttpURLConnection.HTTP_MOVED_PERM || responseCode==HttpURLConnection.HTTP_MOVED_TEMP || responseCode==HttpURLConnection.HTTP_SEE_OTHER || responseCode==307) {
                String location = conn.getHeaderField("Location");
                if(location!=null && !location.equals("")) {
                        System.out.println(urlString + " - " + responseCode + " - new URL: " + location);
                        return get(location);
                } else {
                        throw new IOException(urlString + " : " + conn.getResponseMessage() + "("+responseCode+") : Received moved answer but no Location. exiting.");
                }
        } else {
                is = conn.getErrorStream();
        }
        
        if(is==null) {
                throw new IOException(urlString + " : " + conn.getResponseMessage() + "("+responseCode+") : InputStream was null : exiting.");
        }
        
        String result="";
        try {
                // Get the response
                BufferedReader rd = new BufferedReader(new InputStreamReader(is));
                
                StringBuffer sb = new StringBuffer();
                String line;
                while ((line = rd.readLine()) != null) {
                        sb.append(line + "\n\r");
                }
                rd.close();
                result = sb.toString();
        } catch (Exception e) {
                throw new IOException(urlString + " - " + conn.getResponseMessage() + "("+responseCode+") - " +e.getLocalizedMessage());
        }
        return result;
}
}
